package com.example.xjttoboxuegu.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjttoboxuegu.R;
import com.example.xjttoboxuegu.util.MD5Util;

public class LoginActivity extends AppCompatActivity {

    private TextView tv_main_title;
    private TextView tv_back,tv_register,tv_find_pwd;
    private Button btn_login;
    private String userName,pwd,spPwd;
    private EditText et_user_name,et_pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    /**
     * 获取界面控件
     */
    private void init() {
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("登录");
        tv_back = findViewById(R.id.tv_back);
        tv_register = findViewById(R.id.tv_register);
        tv_find_pwd = findViewById(R.id.tv_find_pwd);
        btn_login = findViewById(R.id.btn_login);
        et_user_name = findViewById(R.id.et_user_name);
        et_pwd = findViewById(R.id.et_pwd);

        //返回按钮的点击事件
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginActivity.this.finish();
            }
        });

        //立即注册控件事件
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });

        //找回密码控件事件
        tv_find_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到找回密码界面
                Intent intent = new Intent(LoginActivity.this, FindPswActivity.class);
                startActivity(intent);
            }
        });

        //登录按钮控件事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName = et_user_name.getText().toString().trim();
                pwd = et_pwd.getText().toString().trim();
                String md5Pwd = MD5Util.md5(pwd);
                spPwd = readPwd(userName);
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if (md5Pwd.equals(spPwd)) {
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    //保存登录状态和登录用户名
                    saveLoginStatus(true,userName);
                    //传递登录状态
                    Intent data = new Intent();
                    data.putExtra("userName",userName);
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    LoginActivity.this.finish();
                    return;
                }else if (!TextUtils.isEmpty(spPwd)  && !md5Pwd.equals(spPwd)) {
                    Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Toast.makeText(LoginActivity.this,"用户不存在",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 从SharedPreferences获取用户名和密码
     * @param userName
     * @return
     */
    private String readPwd(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        return sp.getString(userName,"");
    }

    /**
     * 保存登录状态
     * @param status
     * @param userName
     */
    private void saveLoginStatus(boolean status,String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin",status);
        editor.putString("loginUserName",userName);
        editor.commit();
    }

    /**
     * 接收从注册界面传过来的值
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String userName = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)) {
                et_user_name.setText(userName);
                et_user_name.setSelection(userName.length());
            }
        }
    }
}